/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import org.openeccos.EccosDesktop;
import org.openeccos.properties.PDMessageBoxProperties;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.layout.ColumnLayoutData;
import echopointng.ButtonEx;

/**
 * A model box displaying a simple message
 *
 * @author cgspinner@web.de
 */
public class PDMessageBox extends PDDialog {

	public static void confirmMsgBox(ActionListener listener2) {
		String msg = PDMessageBoxProperties.proceedDeletion();
		confirmMsgBox(msg, listener2);
	}

	public static void confirmMsgBox(String msg, ActionListener listener2) {
		PDMessageBox box = new PDMessageBox(PDMessageBoxProperties.warning(), msg, listener2, 300, 150);
		EccosDesktop.getDesktop().addWindow(box);
	}

	public static void msgBox(String title, String text, int w, int h) {
		try {
			PDMessageBox box = new PDMessageBox(title, text, null, w, h);
			EccosDesktop.getDesktop().addWindow(box);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private ActionListener listener;
	private final String msg;

	public PDMessageBox(String title, String msg, ActionListener l, int w, int h) {
		super(title);
		setWidth(new Extent(w));
		setHeight(new Extent(h));

		this.msg = msg;
		this.listener = l;
		setResizable(false);
		setModal(true);

		initGUI();
	}

	private void initGUI() {
		Column col = new Column();
		col.setCellSpacing(new Extent(15));
		col.setInsets(new Insets(5, 5, 5, 5));
		add(col);

		ColumnLayoutData cld;

		Label lblText = new Label(msg);
		cld = new ColumnLayoutData();
		cld.setAlignment(new Alignment(Alignment.CENTER, Alignment.DEFAULT));
		lblText.setLayoutData(cld);
		col.add(lblText);

		Row row = new Row();
		row.setCellSpacing(new Extent(5));
		cld = new ColumnLayoutData();
		cld.setAlignment(new Alignment(Alignment.CENTER, Alignment.DEFAULT));
		row.setLayoutData(cld);
		col.add(row);

		Button btnOk = new Button(PDMessageBoxProperties.ok());
		btnOk.setAlignment(new Alignment(Alignment.CENTER, Alignment.DEFAULT));
		btnOk.setWidth(new Extent(60));
		btnOk.setStyle(ButtonEx.DEFAULT_STYLE);
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if (listener != null) {
					listener.actionPerformed(evt);
				}
				userClose();
			}
		});
		row.add(btnOk);

		if (listener != null) {
			Button btnCancel = new Button(PDMessageBoxProperties.cancel());
			btnCancel.setAlignment(new Alignment(Alignment.CENTER, Alignment.DEFAULT));
			btnCancel.setWidth(new Extent(80));
			btnCancel.setStyle(ButtonEx.DEFAULT_STYLE);
			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					userClose();
				}
			});
			row.add(btnCancel);
		}
	}

	public void setActionListener(ActionListener l) {
		listener = l;
	}
}
